package com.dchh.android.suverynew.views;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.dchh.android.suverynew.R;
import com.dhcc.android.base.common.bean.KeyValueBean;
import com.dhcc.android.base.common.view.MyPopupWindow;

import java.util.ArrayList;
import java.util.List;

/**
 * 单选
 *
 * @author zhoudan
 * @date 2015年8月21日
 */
public class SelectPoupWindow {

    private MyPopupWindow window;

    private ListView listview;

    private ArrayAdapter<String> adapter;

    private List<String> list = new ArrayList<String>();

    private Activity activity;

    private OnSelectListener listener;

    public SelectPoupWindow(Activity activity) {
        this.activity = activity;
    }

    //===================字符串===================

    public void show(View parent, final TextView target, List<String> data) {

        if (window == null) {
            View v = LayoutInflater.from(activity).inflate(R.layout.util_tool_select_list, null);
            listview = (ListView) v.findViewById(R.id.select_listview);

            adapter = new ArrayAdapter<String>(activity, R.layout.util_tool_select_list_item, R.id.title, list);
            listview.setAdapter(adapter);

            listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String str = list.get(position);
                    target.setText(str);
                    window.dismiss();
                }
            });

            window = new MyPopupWindow(v, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
        }

        list.removeAll(list);
        list.addAll(data);
        adapter.notifyDataSetChanged();

        window.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    public void show(View parent, final OnSelectListener listener, List<String> data) {
        this.listener = listener;
        if (window == null) {
            View v = LayoutInflater.from(activity).inflate(R.layout.util_tool_select_list, null);
            listview = (ListView) v.findViewById(R.id.select_listview);

            adapter = new ArrayAdapter<String>(activity, R.layout.util_tool_select_list_item, R.id.title, list);
            listview.setAdapter(adapter);

            listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String str = list.get(position);
                    window.dismiss();
                    if (listener != null) {
                        listener.onSelectString(view, str);
                    }
                }
            });

            window = new MyPopupWindow(v, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
        }

        list.removeAll(list);
        list.addAll(data);
        adapter.notifyDataSetChanged();

        window.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }

    //============================KeyValueBean==============================


    public void show(View parent, final OnSelectListener listener, KeyValueBean kv, final List<KeyValueBean> datas) {
        this.listener = listener;

        if (window == null) {
            View v = LayoutInflater.from(activity).inflate(R.layout.util_tool_select_list, null);
            listview = (ListView) v.findViewById(R.id.select_listview);

            adapter = new ArrayAdapter<String>(activity, R.layout.util_tool_select_list_item, R.id.title, list);
            listview.setAdapter(adapter);

            listview.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    KeyValueBean item = datas.get(position);
                    window.dismiss();
                    if (listener != null) {
                        listener.onSelectKeyValueBean(view, item);
                    }
                }
            });

            window = new MyPopupWindow(v, LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, true);
        }

        list.clear();
        for (KeyValueBean kvb : datas) {
            list.add(kvb.getValue());
        }
        adapter.notifyDataSetChanged();

        window.showAtLocation(parent, Gravity.CENTER, 0, 0);
    }


    public void dismiss() {
        if (window != null) {
            window.dismiss();
        }
    }

    public interface OnSelectListener {
        public abstract void onSelectString(View view, String value);

        public abstract void onSelectKeyValueBean(View view, KeyValueBean kv);
    }
}
